package com.skt.common_hr.medicalmoney.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.medicalmoney.MedicalMoneyFacade;
	import com.skt.common_hr.medicalmoney.model.vo.MedicalMoneyAcountVO;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class AccountProxy extends CommonProxy
	{
		public static const NAME:String = "AccountProxy";
		
		public var AccountList:ArrayCollection;
		public var AccountBank:ArrayCollection = null;

		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function AccountProxy(data:Object=null)
		{
			super(NAME, data);
		}


		//계좌선택콤보
		public function loadAccounts(pernr:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;

			loadData("Z_HR_0042", param, bnksaResult, bnksaFault);
		}

		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 기타계좌 콤보박스 조회
		 * @param
		 * @return void
		 */
		public function loadBankList():void
		{
			var param:Object = new Object();
			loadData("Z_HR_0051",param, bankCodeResult, bankCodeFault);
		}


		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 		- 계좌선택유콤보
		 * @param
		 * @return void
		 */
		public function bnksaResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				AccountList = parseBankInfo(event.result.BTAB);
				facade.sendNotification(MedicalMoneyFacade.LOAD_MEDICALMONEY_BANKA_COMPLETE, this);
			}
			else {
				facade.sendNotification(MedicalMoneyFacade.LOAD_MEDICALMONEY_BANKA_FAILED, this);
			}
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 * 		- 계좌선택유콤보
		 * @param
		 * @return void
		 */
		private function bnksaFault(event:FaultEvent):void
		{
			facade.sendNotification(MedicalMoneyFacade.LOAD_MEDICALMONEY_BANKA_FAILED, this);
		}


		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 기타계좌 콤보박스 데이터
		 *
		 * @param
		 * @return void
		 */
		public function bankCodeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				AccountBank = bankCodeList(event.result.BTAB);
				facade.sendNotification(MedicalMoneyFacade.LOAD_BANKCODE_COMPLETE, this);
			}
			else {
				facade.sendNotification(MedicalMoneyFacade.LOAD_BANKCODE_FAILED, this);
			}
		}
		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function bankCodeFault(event:FaultEvent):void
		{
			facade.sendNotification(MedicalMoneyFacade.LOAD_BANKCODE_FAILED, this);
		}


		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 *
		 * @param ArrayCollection
		 * @return 계좌선택  ArrayCollection
		 */
		private function parseBankInfo(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			var vo:MedicalMoneyAcountVO = new MedicalMoneyAcountVO();

			vo.BNKSA = "";
			vo.STEXT = "";
			vo.BANKL = "";
			vo.BANKA = "";
			vo.BANKN = "";
			vo.EMFTX = "";
			vo.BANKS = "";
			retArr.addItem(vo);
			for each(var rec:Object in srcArr)
			{
				vo = new MedicalMoneyAcountVO();
				vo.BNKSA = rec["BNKSA"];
				vo.STEXT = rec["STEXT"];
				vo.BANKL = rec["BANKL"];
				vo.BANKA = rec["BANKA"];
				vo.BANKN = rec["BANKN"];
				vo.EMFTX = rec["EMFTX"];
				vo.BANKS = rec["BANKS"];

				retArr.addItem(vo);
			}
			return retArr;
		}
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 *
		 * @param
		 * @return 선택적복리후생 초기화면 list
		 */
		private function bankCodeList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var rec:Object in srcArr)
			{
				retArr.addItem({label:rec["BANKA"], data:rec["BANKL"]});
			}
			return retArr;
		}
	}
}